package com.ruoyi.web.controller.evaluation;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.evaluation.Evaluation;
import com.ruoyi.system.domain.evaluation.EvaluationEnity;
import com.ruoyi.system.service.evaluationService.EvaluationService;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Controller
@RequestMapping("/evaluation")
public class EvaluationController extends BaseController {
    @Autowired
    private EvaluationService evaluationService;
    private String prefix = "evaluation/";

    /**
     * 跳转评价页面
     * @return
     */
    @RequiresPermissions("evaluation:assess:view")
    @GetMapping("/evaluationList")
    public String listEvaluation(){

        return prefix+"list";
    }

    /**
     * 返回评价数据
     * @return
     */
    @RequiresPermissions("evaluation:assess:view")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(){
        startPage();
        //List<Evaluation> evaluations = evaluationService.selectEvaluation();
        List<EvaluationEnity> evaluationEnities =evaluationService.selectEvaluationName();
        return getDataTable(evaluationEnities);
    }

    /**
     * 返回评价数据
     * @return
     */
    @RequiresPermissions("evaluation:assess:view")
    @PostMapping("/list2")
    @ResponseBody
    public String  list2(){
        //用户评分信息
        List<EvaluationEnity> evaluations = evaluationService.selectEvaluation2();
        Map out = new TreeMap();
        out.put("evaluations",evaluations);
        JSONObject jsonObject = JSONObject.fromObject(out);
        return jsonObject.toString();
    }

    /**
     * 删除全部评分为零的评价
     * @return
     */
    @RequiresPermissions("evaluation:assess:removeAll")
    @PostMapping("/deleteScore")
    @ResponseBody
    public AjaxResult deleteScore(){
        int num = evaluationService.deleteEvaluationByScore();
        if (num>0){
            return success();
        }
        else{
            return  error("没有评价为零的数据了哦");
        }
    }

    /**
     * 删除评价
     * @param ids
     * @return
     */
    @RequiresPermissions("evaluation:assess:view")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            //根据删除story的数据
            int num = evaluationService.deleteEvaluationById(ids);
            if (num>0){
                return success();
            }else{
                return error("删除失败哦！！");
            }
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }


}
